package util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtils {
    /**
     * 将javabean对象转换为Jason字符串
     * @param obj
     * @param<T>
     * @return
     */
    public static<T> String javaBean2JsonStr(T obj) {
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
        public static<T> T jsonStr2JavaBean(String jsonStr , Class<T> clazz){
            try {
                return  new ObjectMapper().readValue(jsonStr,clazz);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
